<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="仓库名称" prop="storeName">
                <el-input
                        v-model="queryParams.storeName"
                        placeholder="请输入仓库名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="公司名称" prop="companyName">
                <el-input
                        v-model="queryParams.companyName"
                        placeholder="请输入公司名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="所在城市" prop="storeCity">
                <el-input
                        v-model="queryParams.storeCity"
                        placeholder="请输入所在城市"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" border :data="storeList">
            <el-table-column label="序号" align="center" type="index" width="60"/>
            <el-table-column label="仓库名称" align="center" prop="storeName" :show-overflow-tooltip="true"/>
            <el-table-column label="所属公司" align="center" prop="companyName" :show-overflow-tooltip="true"/>
            <el-table-column label="所在城市" align="center" prop="storeCity" width="100"/>
            <el-table-column label="具体地址" align="center" prop="storeAddress" :show-overflow-tooltip="true"/>
            <el-table-column label="仓库容量" align="center" prop="storeVolume" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{scope.row.storeVolume}}立方米</span>
                </template>
            </el-table-column>
            <el-table-column label="使用率" align="center" prop="storeUtilizationRate" width="80">
                <template slot-scope="scope">
                    <span>{{scope.row.storeUtilizationRate}}%</span>
                </template>
            </el-table-column>
            <el-table-column label="操作时间" align="center" prop="updateTime" width="160">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['store:modifyStore']"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['store:delStore']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改岗位对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
                <el-form-item label="仓库名称" prop="storeName">
                    <el-input v-model="formData.storeName" placeholder="请输入仓库名称标识" show-word-limit clearable
                              :style="{width: '100%'}"></el-input>
                </el-form-item>
                <el-form-item label="所属公司" prop="companyName">
                    <el-input v-model="formData.companyName" placeholder="请输入仓库所属公司" maxlength="20">
                        <el-button slot="append" icon="el-icon-search" @click="queryCompany"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item label="所在城市" prop="storeCity">
                    <el-input v-model="formData.storeCity" placeholder="请输入仓库所在城市" show-word-limit clearable
                              :style="{width: '100%'}"></el-input>
                </el-form-item>
                <el-form-item label="具体地址" prop="storeAddress">
                    <el-input v-model="formData.storeAddress" placeholder="请输入仓库具体地址" show-word-limit clearable
                              :style="{width: '100%'}"></el-input>
                </el-form-item>
                <el-form-item label="仓库容积" prop="storeVolume">
                    <el-input v-model="formData.storeVolume" placeholder="请输入仓库容积" show-word-limit clearable
                              :style="{width: '100%'}">
                        <template slot="append">立方米</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="房间数" prop="houseNumber">
                    <el-input-number v-model="formData.houseNumber" controls-position="right" :min="1"/>
                </el-form-item>
                <el-form-item label="使用率%" prop="storeUtilizationRate">
                    <el-input-number v-model="formData.storeUtilizationRate" controls-position="right" :min="0">
                    </el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!--查询公司信息对话框-->
        <el-dialog title="选择公司信息"  :visible.sync="queryCompanyOpen" width="600px" append-to-body>
            <el-table
                    border
                    :row-style="{height:0+'px'}"
                    :cell-style="{padding:0+'px'}"
                    :data="companyList"
                    default-expand-all>
                <el-table-column prop="name" label="公司名" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="idCode" label="注册码" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="type" label="公司类型"></el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="checkCompany(scope.row)"
                        >选择
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                    v-show="companyTotal>0"
                    :total="companyTotal"
                    :page.sync="queryCompanyParams.pageNum"
                    :limit.sync="queryCompanyParams.pageSize"
                    @pagination="getCompanyList"
            />
            <div slot="footer" class="dialog-footer">
                <el-button @click="queryCompanyCancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {listStore,modifyStore,delStore} from "@/api/store/store.js";
    import {listCompany} from "@/api/organization/company.js";

    export default {
        name: "storeList",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 总条数
                total: 0,
                // 仓库表格数据
                storeList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    storeName: undefined,
                    companyName: undefined,
                    storeCity: undefined
                },
                // 表单参数
                formData: {
                    storeId: undefined,
                    companyId: undefined,
                    companyName: undefined,
                    storeEntityId: undefined,
                    storeName: undefined,
                    storeAddress: undefined,
                    storeCity: undefined,
                    houseNumber: undefined,
                    storeVolume: undefined,
                    storeUtilizationRate: undefined,
                },
                queryCompanyOpen: false,
                queryCompanyParams: {
                    pageNum: 1,
                    pageSize: 10,
                    name: undefined
                },
                companyList: [],
                companyTotal: 0,
                // 表单校验
                rules: {
                    storeName: [{
                        required: true,
                        message: '请输入仓库名称',
                        trigger: 'blur'
                    }],
                    companyName: [{
                        required: true,
                        message: '请输入查询所属公司信息',
                        trigger: 'blur'
                    }],
                    storeCity: [{
                        required: true,
                        message: '请输入仓库所在城市',
                        trigger: 'blur'
                    }],
                    storeAddress: [{
                        required: true,
                        message: '请输入仓库具体地址',
                        trigger: 'blur'
                    }]
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询仓库列表 */
            getList() {
                this.loading = true;
                listStore(this.queryParams).then(response => {
                    this.storeList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.formData = {
                    storeId: undefined,
                    companyId: undefined,
                    companyName: undefined,
                    storeEntityId: undefined,
                    storeName: undefined,
                    storeAddress: undefined,
                    storeCity: undefined,
                    houseNumber: undefined,
                    storeVolume: undefined,
                    storeUtilizationRate: undefined,
                };
                this.resetForm("form");
            },
            /** 查询按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.formData = Object.assign({}, row);
                this.title = "修改仓库信息";
                this.open = true;
            },
            queryCompany(){
                if (this.formData.companyName == null || this.formData.companyName == undefined || this.formData.companyName == '') {
                    this.msgError("公司名称不能为空！");
                    return;
                } else {
                    this.queryCompanyParams.name = this.formData.companyName;
                    this.getCompanyList();
                    this.queryCompanyOpen = true;
                }
            },
            getCompanyList(){
                listCompany(this.queryCompanyParams).then(response => {
                    this.companyList = response.rows;
                    this.companyTotal = response.total;
                });
            },
            checkCompany(row){
                this.formData.companyName = row.name;
                this.formData.companyId = row.companyId;
                this.queryCompanyOpen = false;
            },
            queryCompanyCancel() {
                this.queryCompanyOpen = false;
            },
            /** 提交按钮 */
            submitForm: function () {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        modifyStore(this.formData).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                let app = this;
                app.$confirm('是否确认删除名称为"' + row.storeName + '"的仓库?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    let delData = {
                        storeId: row.storeId,
                        delFlag: '1'
                    }
                    delStore(delData).then(response => {
                        if (response.code === 200) {
                            app.msgSuccess("删除成功");
                            app.getList();
                        } else {
                            app.msgError(response.msg);
                        }
                    });
                });
            }
        }
    };
</script>

